﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RoleMenu.aspx.cs" Inherits="MagicBag.CRM.Web.Admin.RoleMenu" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
    <style type="text/css">
        *
        {
            font-size: 12px;
        }
    </style>
    <link href="../Script/jquery-easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../Script/jquery-easyui/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../Script/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="../Script/jquery-easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../Script/json2.js" type="text/javascript"></script>
    <script type="text/javascript">
        function Save() {
            var roleID = $('#Role').combobox('getValue');
            if (roleID == "") {
                $.messager.alert("提示", "请选择角色！", "error");
                return;
            }
            var nodes = $('#tt2').tree('getChecked');

            //得到选中的菜单ID
            var ids = $.map(nodes, function (n) {
                if ($('#tt2').tree('isLeaf', n.target))//返回叶子节点
                    return n.id;
            });

            if (ids.length == 0) {
                var roleName = $('#Role').combobox('getText');
                $.messager.alert("提示", "请选择要分配给【<font color='red'>" + roleName + "</font>】的菜单！", "error");
                return;
            }

            var menuIDs = JSON.stringify(ids); //IE8+中javascript自带，本系统使用json2以便兼容ie6,ie7
            $.post('MagicBag.CRM.Service/RoleMenuService.crm?act=Save', { roleID: roleID, menuIDs: menuIDs }, function (result) {
                if (result.Success) {
                    $.messager.alert("提示", "菜单分配完成", "info");
                    $('#tt2').tree('reload'); // reload the user data
                    //重新初始化
                    InitMenu();
                } else {
                    $.messager.alert("错误", result.Msg, "error");
                }
            }, 'json');
        }

        //初始化菜单
        function InitMenu() {
            var roleID = $('#Role').combobox('getValue');
            var url = 'MagicBag.CRM.Service/RoleMenuService.crm?act=GetRoleMenuIDsByRoleID';
            $.post(url, { roleID: roleID }, function (data) {
                if (data != null && data.length > 0) {
                    $(data).each(function () {
                        var node = $('#tt2').tree('find', this); //找到当前id的节点
                        if (node != undefined)
                            $('#tt2').tree('check', node.target); //选中节点
                    });
                }
            }, 'json');
        }

        $(function () {
            $('#Role').combobox({
                url: 'MagicBag.CRM.Service/RoleService.crm?act=GetRoles&ddl=1',
                valueField: 'ID',
                textField: 'Name',
                filter: function (q, row) {
                    var opts = $(this).combobox('options');
                    return row[opts.textField].indexOf(q) != -1;
                },
                onSelect: function (row) {
                    //改变角色后,重新加载
                    $("#tt2").tree("reload");
                    //重新初始化
                    InitMenu();
                }

            });


        });
    </script>
</head>
<body>
    <div style="border: 1px dashed gray; padding: 5px;">
        角色：<input type="text" id="Role" style="width: 150px;" />
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="Save()">
            保存</a>
    </div>
    <ul id="tt2" style="padding: 10px;" class="easyui-tree" data-options="url:'MagicBag.CRM.Service/MenuService.crm?act=GetMenus',checkbox:true,
			onClick: function(node){
				$(this).tree('toggle', node.target);
			}">
    </ul>
</body>
</html>
